<template>
	<view class="payOrder">
		<view class="money">
			<view class="top">
				<view style="color: #989898;">订单金额</view>
				<u-count-down :time="countDown" format="HH:mm:ss" @change="onChange"></u-count-down>
				<view><text>{{obj.orderTotal}}</text><text style="font-size: 22rpx;">元</text></view>
			</view>
			<view class="top">
				<view style="color: #989898;">优惠金额</view>
				<view style="color: #fbc21a;"><text>-￥{{obj.discountTotal}}</text></view>
			</view>
			<view class="top" v-if="obj.ruleDiscountTotal > 0">
				<view style="color: #989898;">砍价金额</view>
				<view style="color: red;"><text>-￥{{obj.ruleDiscountTotal}}</text></view>
			</view>
			<view class="middle">
				<view style="padding-bottom: 20rpx;font-size: 22rpx;color: #989898;">支付金额</view>
				<view>
					<text style="font-size: 32rpx;color: #3E56BA;">￥</text>
					<text style="font-size: 48rpx;color: #3E56BA;">{{obj.total}}</text>
				</view>
			</view>
			<view class="bottom" v-if="shareHand && obj.isCut && obj.total > 0">
				<u-button open-type="share" type="primary" text="发起砍价"></u-button>
			</view>
		</view>
		<view class="order_detail">
			<view class="order_title">
				检验信息
			</view>
			<view class="order_item">
				<view class="item_label">委托方</view>
				<view class="item_content">{{obj.client}}</view>
			</view>
			<view class="order_item">
				<view class="item_label">型号</view>
				<view class="item_content">{{obj.model}}</view>
			</view>
			<view class="order_item" v-if="obj.addressType">
				<view class="item_label">品牌</view>
				<view class="item_content">{{obj.brandName || '无'}}</view>
			</view>
			<view class="order_item" v-if="obj.addressType">
				<view class="item_label">供应商</view>
				<view class="item_content">{{obj.supplier || "无"}}</view>
			</view>
			<view class="order_item" v-if="obj.addressType">
				<view class="item_label">批次</view>
				<view class="item_content">{{obj.batch || '无'}}</view>
			</view>
			<view class="order_item" v-if="obj.addressType">
				<view class="item_label">产地</view>
				<view class="item_content">{{obj.production || '无'}}</view>
			</view>
			<view class="order_item" v-if="obj.addressType">
				<view class="item_label">送检数量</view>
				<view class="item_content">{{obj.sCount}}</view>
			</view>
			<view class="order_item" v-if="obj.addressType">
				<view class="item_label">检测报告</view>
				<view class="item_content">{{obj.cne}}</view>
			</view>
			<view class="order_item" v-if="obj.addressType">
				<view class="item_label">是否加急</view>
				<view class="item_content">{{obj.urgent == 1 ? '是' : '否'}}</view>
			</view>
			<view class="order_item">
				<view class="item_label">备注</view>
				<view class="item_content">{{obj.remark || '无'}}</view>
			</view>
			<view class="order_item" v-if="obj.addressType">
				<view class="item_label">寄回方式</view>
				<view class="item_content">{{textAddrssType(obj.addressType) }}</view>
			</view>
			<view class="project">
				<view class="item_label">检测项目</view>
				<view class="pro_content">
					<view class="item_pro" v-for="(item,index) in obj.commodityJson" :key="index">
						{{item.Name}}：{{item.Price}}元
					</view>
				</view>
			</view>
		</view>
		<view class="pay">
			<view class="title">支付方式</view>
			<view class="content">
				<u-radio-group v-model="payType" placement="column" size="22">
					<u-radio :customStyle="{marginBottom: '24rpx'}" :name="1" iconSize="20" v-if="obj.total !== 0">
						<view style="display: flex;align-items: center;">
							<image style="width: 42rpx;margin-left: 20rpx;" src="http://applet.atptest.com/23042301.png"
								mode="widthFix">
							</image>
							<view style="font-size: 28rpx;">微信支付</view>
						</view>
					</u-radio>
					<u-radio :customStyle="{marginBottom: '8px'}" :name="2" iconSize="20">
						<view style="display: flex;align-items: center;">
							<image style="width: 42rpx;margin-left: 20rpx;" src="http://applet.atptest.com/23042302.png"
								mode="widthFix"></image>
							<view style="font-size: 28rpx;display: flex;align-items: center;">
								<text style="margin-right:30rpx;">余额支付</text>
								<text style="color: #868686;font-size: 24rpx;">账户余额：{{obj.balance}}</text>
							</view>
						</view>
					</u-radio>
				</u-radio-group>
			</view>
		</view>

		<view class="submit_container">
			<button @click="toPaySubmit()">立即支付</button>
		</view>
	</view>
</template>

<script>
	import {
		PaymentOrderDetails,
		PaySubmit
	} from '../../../api/balance.js'
	import {
		GetPayPermTest222
	} from '../../../api/user.js'
	import {
		shareFlag
	} from "../../../api/bargain.js"
	export default {
		data() {
			return {
				obj: {},
				// 订单号
				orderNo: '',
				// 倒计时
				countDown: 86400000,
				timer: '',
				shareHand: false, //是否发起砍价
				payFlag: true,
				payType: 2, //支付方式
			};
		},
		onLoad: function(option) {
			this.orderNo = option.i
		},
		onShow() {
			// 根据订单编号获取订单支付详情
			PaymentOrderDetails({
				orderNo: this.orderNo
			}).then((res) => {
				console.log("订单信息",res);
				this.obj = res
				this.countDown = 86400000 - (parseInt(new Date() - new Date(this.obj.createTime)))
				if (res.cne == 1) {
					this.obj.cne = "中文报告"
				} else if (res.cne == 2) {
					this.obj.cne = "英文报告"
				} else if (res.cne == 3) {
					this.obj.cne = "中文报告 + 英文报告"
				} else {
					this.obj.cne = "无"
				}
				this.obj.commodityJson = JSON.parse(this.obj.commodityJson)
			})

			//能否发起砍价
			this.shareRule()
		},
		onHide() {
			clearTimeout(this.timer)
		},
		methods: {
			//寄回方式
			textAddrssType(val) {
				switch (val) {
					case 1:
						return "自行邮寄"
					case 2:
						return "到店检测(寄回)"
					case 4:
						return "到店检测(自提)"
				}
			},

			//能否发起砍价
			async shareRule() {
				const res = await shareFlag()
				this.shareHand = res.hand
			},

			// 去优惠券选择界面
			toDiscount() {
				uni.navigateTo({
					url: `/pagesA/pages/discountSelect/discountSelect?i=${this.obj.id}`
				})
			},

			//自定义分享 当前只针对自定义分享时触发的情况，如果时点击小程序自带三个点分享页面的，需根据e来判断点击类型进行分享操作！
			async onShareAppMessage(e) {
				return {
					title: '点击为好友砍价',
					path: `/pagesB/pages/bargain/bargain?orderId=${this.obj.id}`,
					desc: '将订单转发给好友进行砍价',
					imageUrl: 'http://applet.atptest.com/JlrzC9db1719997453657.jpg?v=1719997454121',
				}
			},

			// 支付提交
			toPaySubmit() {
				let that = this
				console.log("that.payType", that.payType);
				uni.showModal({
					title: '提示',
					content: `确认使用${that.payType == 1 ? '微信' : '余额'}支付吗？`,
					success(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '加载中'
							})
							if (that.payType == 1) { //微信支付
								that.toPay()
							} else { //余额支付
								that.balancePay()
							}
						} else if (res.cancel) {
							console.log('取消');
						}
					}
				})
			},

			// 获微信支付数据
			toPay() {
				GetPayPermTest222({
					orderNo: this.orderNo
				}).then((res) => {
					const _that = this
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res.timeStamp,
						nonceStr: res.nonceStr,
						package: res.package,
						signType: res.signType,
						paySign: res.paySign,
						success: function(res) {
							// 成功回调
							uni.showToast({
								title: '下单成功！！',
								duration: 2000,
							});
							_that.payFlag = true
							uni.reLaunch({
								url: '/pagesB/pages/order/order?i=3'
							})
						},
						fail: function(err) {
							uni.showToast({
								title: '取消支付',
								duration: 2000,
								icon: "error"
							});
							_that.payFlag = true
							console.log('fail:' + JSON.stringify(err));
						}
					});
				})
			},

			//余额支付
			balancePay() {
				PaySubmit({
					orderNo: this.orderNo
				}).then((res) => {
					if (res === true) {
						uni.hideLoading();
						uni.showToast({
							duration: 3000,
							title: '支付成功！'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pagesB/pages/order/order?i=3'
							})
						}, 1000)
					}
				})
			},

			//倒计时
			onChange(e) {
				if (e.days == 0 && e.hours == 0 && e.minutes == 0 && e.seconds == 0 && e.milliseconds == 0) {
					uni.showToast({
						title: "订单已过期，请重新下单",
						icon: 'none'
					})

					this.timer = setTimeout(function() {
						uni.hideLoading();
						uni.redirectTo({
							url: '/pages/test/test'
						})
					}, 2000);
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.payOrder {
		padding: 30rpx 30rpx 100rpx 30rpx;
		box-sizing: border-box;
		min-height: 100vh;
		background-color: #f5f5f5;

		.money {
			background-color: #fff;
			border-radius: 12rpx;

			// box-sizing: border-box;
			.top {
				display: flex;
				justify-content: space-between;
				padding: 26rpx 20rpx 30rpx;
			}

			.middle {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-top: 1px solid #F4F4F4;
				border-bottom: 1px solid #F4F4F4;
				padding: 30rpx 0;
			}
		}

		.order_detail {
			background-color: #fff;
			border-radius: 12rpx;
			padding: 20rpx;
			box-sizing: border-box;
			margin-top: 20rpx;

			.order_title {
				color: #2B2B2B;
				font-weight: bold;
				margin-bottom: 20rpx;
			}

			.order_item {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx;
				box-sizing: border-box;
				border-bottom: 1px solid #d8d8d8;

				.item_label {
					font-size: 28rpx;
					min-width: 160rpx;
				}

				.item_content {
					font-size: 24rpx;
				}
			}

			.project {
				width: 100%;
				padding: 20rpx;
				box-sizing: border-box;

				.item_label {
					font-size: 28rpx;
				}

				.pro_content {
					display: flex;
					align-items: center;
					flex-wrap: wrap;

					.item_pro {
						padding: 8rpx 20rpx;
						box-sizing: border-box;
						color: #fff;
						font-size: 24rpx;
						margin: 10rpx;
						margin-left: 0;
						border-radius: 30rpx;
						background: rgb(62, 86, 186);
					}
				}
			}
		}

		.bottom {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 28rpx 20rpx;

			.bargain_btn {
				background-color: #3e56ba;
				color: #fff;
			}
		}

		.pay {
			margin-top: 30rpx;
			background-color: #fff;
			border-radius: 12rpx;
			padding: 20rpx;
			box-sizing: border-box;

			.title {
				color: #3E56BA;
			}

			.content {
				margin-top: 20rpx;
			}
		}

		.submit_container {
			width: 100%;
			margin-top: 20rpx;

			button {
				background-color: #23a8f2;
				color: #fff;
			}
		}
	}

	/deep/ .u-count-down__text {
		color: #BB645A !important;
	}
</style>